﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevComponents.DotNetBar;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Forms.Utils;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class CustomerPaymentView : BaseUserControl
    {
        public CustomerPaymentView()
        {
            InitializeComponent();
            InitCustomerPayment();
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
            customerPaymentSearchControl1.PaymentSearch += this.PaymentSearch;
            
        }

        public CustomerPaymentView(ClientAppBase app)
            : base(app)
        {
            InitializeComponent();
            InitCustomerPayment();
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
        }

        private void PaymentSearch(string sFilter)
        {
            if (gdPayment.DataSource != null)
            {
                DataView dv = (DataView)gdPayment.DataSource;
                dv.RowFilter = sFilter;
            }
        }

        private void InitCustomerPayment()
        {
            string error = string.Empty;
            string filter = string.Empty;
            DataSet ds = new DataSet();
            if (BussinessProxyManager.Instance == null)
            {
                return;
            }
            bool bResult = BussinessProxyManager.Instance.QueryCustomerPayment(filter, out ds, out error);
            if (bResult == false)
            {
                MsgBox.MsgInfo("获取会员充值信息失败\r\n" + error);
                return;
            }
            gdPayment.DataSource = null;
            gdPayment.DataSource = ds.Tables[0].DefaultView;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddCustomerPayment form = new frmAddCustomerPayment();
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Text = "新增会员充值信息";
            form.Flag = ControlClassModeEnum.emInsert;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerPayment();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iHandle = gvPayment.FocusedRowHandle;
            if (iHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的充值信息。");
                return;
            }
            DataRow dr = gvPayment.GetDataRow(iHandle);
            DBEntityCustomerPayment entity = new DBEntityCustomerPayment(dr);
            frmAddCustomerPayment form = new frmAddCustomerPayment(entity);
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Text = "修改会员充值信息";
            form.Flag = ControlClassModeEnum.emEdit;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerPayment();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            int iHandle = gvPayment.FocusedRowHandle;
            if (iHandle < 0)
            {
                MsgBox.MsgInfo("请选择删除的充值信息。");
                return;
            }
            bool bResult = MsgBox.MsgQue("确定要删除选中的记录吗？");
            if (bResult == true)
            {
                DataRow dr = gvPayment.GetDataRow(iHandle);
                int payment_id = Convert.ToInt32(dr[DBEntityCustomerPayment.FieldName.CPAYMENTID]);
                bool bFlag = BussinessProxyManager.Instance.DeleteCustomerPayment(payment_id, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("删除会员充值信息失败\r\n" + error);
                    return;
                }
                InitCustomerPayment();
            }
        }

        private void gvPayment_DoubleClick(object sender, EventArgs e)
        {
            btnModify_Click(null, null);
        }
    }
}
